import constants from '@/constants'

export const PRODUCTS_LIST = {
    data(){
        return{
            PRODUCTS_LIST: [],
			PL_PAGE_NUM: 1,
			PL_LOADING_MORE: false,
			PL_TOP_3_CATES: []
        }
    },
    methods: {
		// TODO: 加分页
        async GetProductListByHome(callback){
            let { data } = await this.$api.GetProductListByHome({
                merchantId: this.$store.state.merchantId,
                pageSize: 100,
				pageNum: this.PL_PAGE_NUM
            })
			
			// 这里遍历下 将 一级目录二级目录下的 商品都取出来
			let GoodsList = []
			Array.isArray(data) && data.map(row => {
				const { productDTOs, sonCategoryProductsDTOs } = row
				Array.isArray(productDTOs) && GoodsList.push(...productDTOs.filter(d => d.isHot))
				if (this.PL_TOP_3_CATES.length < 3) {
					this.PL_TOP_3_CATES.push(row.categoryName)
				}
				Array.isArray(sonCategoryProductsDTOs) && sonCategoryProductsDTOs.map(item=>{
					const { productDTOs } = item
					
					Array.isArray(productDTOs) && GoodsList.push(...productDTOs.filter(d => d.isHot))
				})
				
			})
			
			// this.PRODUCTS_LIST = []
			uni.setStorageSync(constants.HOTSEARCH, this.PL_TOP_3_CATES)
			
			if (!callback) {	
                // this.PRODUCTS_LIST = []
				// this.PRODUCTS_LIST = this.PRODUCTS_LIST.concat(GoodsList)
				this.PRODUCTS_LIST.push(...GoodsList)
			} else {
				callback && callback(GoodsList)
			}
			this.PL_LOADING_MORE = false;
        }
    },
	onReachBottom() {
		// if (!this.PL_LOADING_MORE) {
		// 	this.PL_LOADING_MORE = true;
		// 	this.PL_PAGE_NUM++;
		// 	this.GetProductListByHome()
		// }
	}
}